﻿using System;
using System.ComponentModel.Composition;
using System.Drawing;
using System.Threading.Tasks;
using DeepZoomPivotConstructor.VisualLib;
using IndicoInterface;
using IndicoRender;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RenderEngine;
using RenderEngineInterfaces;

namespace t_IndicoRender
{
    /// <summary>
    /// Summary description for the RenderIndico dude...
    /// </summary>
    [TestClass]
    public class t_RenderIndicoWebSite
    {
        public t_RenderIndicoWebSite()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        /// <summary>
        /// The agent string we use in URI requests!
        /// </summary>
        [Export("HttpAgentString")]
        private string HttpAgentString { get; set; }

        [TestInitialize]
        public void InitForTest()
        {
            HttpAgentString = "RenderIndiocWebSite Test (DeepTalk)";
            t_TestingLibrary.Utilities.MyClassInit();
            t_TestingLibrary.Utilities.AddPart(new RenderControl());
            t_TestingLibrary.Utilities.AddPart(new LayoutHelper());
            t_TestingLibrary.Utilities.AddPart(new Logging.LoggerFile());
        }

        [TestCleanup]
        public void ShutDownTest()
        {
            t_TestingLibrary.Utilities.MyClassDone();
        }

        [Import]
        private IRenderEngine MyRender { get; set; }

        [TestMethod]
        public void TestCanRenderUrl()
        {
            ///
            /// Can we tell the difference between indico and not
            /// We don't have to test this at all levels - because
            /// we have a library we depend on underneath the covers.
            /// So basic tests...
            ///

            RenderIndicoWebSite iw = new RenderIndicoWebSite();
            Assert.AreEqual(RenderEngineInterfaces.RenderPluginPriority.RPPCantDoIt, iw.RenderPriority(new Uri("http://www.nytimes.com")), "nytimes web site shouldn't be renderable!");
            Assert.AreEqual(RenderEngineInterfaces.RenderPluginPriority.RPPSpecific, iw.RenderPriority(new Uri("http://indico.cern.ch/conferenceDisplay.py?confId=14475")), "nytimes web site shouldn't be renderable!");
            Assert.AreEqual(RenderEngineInterfaces.RenderPluginPriority.RPPCantDoIt, iw.RenderPriority(new Uri("https://indico.fnal.gov/getFile.py/access?contribId=318&sessionId=1&resId=0&materialId=slides&confId=3563")), "Contribution id isn't working here");
        }

        /// <summary>
        /// Dummy guy to fake out rendering a meeting
        /// </summary>
        [Export(typeof(IRenderItem))]
        [RenderInfo(typeof(AgendaInfo))]
        private class MyRenderMeeting : IRenderItem
        {
            public bool DidRender = false;
            public System.Threading.Tasks.Task<DeepZoomPivotConstructor.VisualLib.Visual> Render(object what)
            {
                return Task<Visual>.Factory.StartNew(() =>
                    {
                        DidRender = true;
                        return new VisualBitmapImage(1.0, 2.0, 100, Color.Black);
                    });
            }

            public RenderPluginPriority RenderPriority(object what)
            {
                return RenderPluginPriority.RPPGeneric;
            }
        }

        [TestMethod]
        public void TestRenderWebURL()
        {
            /// Can we render a url?
            t_TestingLibrary.Utilities.AddPart(this);
            MyRenderMeeting my = new MyRenderMeeting();
            t_TestingLibrary.Utilities.AddPart(my);

            RenderIndicoWebSite iw = new RenderIndicoWebSite();
            t_TestingLibrary.Utilities.Compose(iw);

            Visual v = iw.Render(new Uri("http://indico.cern.ch/conferenceDisplay.py?confId=a042880")).Result;
            Assert.AreEqual(1, v.SGNodeList.Count, "Wrong number of images came back! should be one bitmap!");
            Assert.IsTrue(my.DidRender, "Did Render didn't get called");
        }
    }
}
